<?php

namespace app\admin\controller;

use api\AliyunSms;

/**
 * @title 系统相关
 *
 */
class System extends Common
{
    /**
     * @title 检查APP更新
     * @desc  {"0":"接口地址：http://api.huaxiaoujia.com/index/System/checkUpdate","1":"请求方式：GET","2":"接口备注：必传参数用于加密验证"}
     * @param {"name":"version","type":"int","required":true,"default":"","desc":"当前应用版本"}
     * @param {"name":"app_type","type":"int","required":true,"default":"","desc":"应用类型1:Android、2:IOS"}
     * @return {"name":"id","type":"int","required":true,"desc":"ID","level":1}
     * @return {"name":"title","type":"string","required":true,"desc":"标题","level":1}
     * @return {"name":"content","type":"string","required":true,"desc":"更新简介","level":1}
     * @return {"name":"version","type":"int","required":true,"desc":"版本","level":1}
     * @return {"name":"url","type":"string","required":true,"desc":"APP下载地址","level":1}
     * @return {"name":"updateType","type":"int","required":true,"desc":"更新类型1:强制更新、2:推荐更新","level":1}
     * @return {"name":"time","type":"string","required":true,"desc":"发布时间","level":1}
     */
    public function checkUpdate()
    {
        $version = input('version'); //客户端版本
        $app_type = input('app_type'); //
        if (!$version || !$app_type) {
            $this->rest("参数错误", "s004");
        }
        $db = db('app_update');
        $field = 'title,content,version,url,update_type as updateType,time';
        $where = array(
            "app_type" => $app_type,
            "version" => array(">", $version)
        );
        //查询大于客户端版本的记录
        $data = $db->field($field)->where($where)->order('version', 'desc')->find();
        //判断是否有强更的记录
        if ($data) {
            $update_type = $db->field($field)->where($where)->order('version', 'desc')->where('update_type', 1)->count();
            if ($update_type > 0) {
                //若果在这之后的任意一个版本需要强制更新,则必须强制更新
                $data['updateType'] = 1;
            }
            $this->rest($data);
        } else {
            $this->rest("暂无更新");
        }

    }

    /**
     * 发送短信
     */
    public function send()
    {
        $params = array();

        // *** 需用户填写部分 ***
        // fixme 必填：是否启用https
        $security = false;

        // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
        $accessKeyId = "LTAIQipRRm7IRKRH";
        $accessKeySecret = "TI4043NRB1bMpKE6uVrJ4zldnF8Dti";

        // fixme 必填: 短信接收号码
        $params["PhoneNumbers"] = "15085290559";

        // fixme 必填: 短信签名，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
        $params["SignName"] = "华夏欧家";

        // fixme 必填: 短信模板Code，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
        $params["TemplateCode"] = "SMS_59940223";

        // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
        $params['TemplateParam'] = Array(
            "code" => "12345",
        );

        // fixme 可选: 设置发送短信流水号
//    $params['OutId'] = "12345";

        // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下，无特殊需求用户请忽略此字段
//    $params['SmsUpExtendCode'] = "1234567";


        // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
        if (!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
        }

        // 初始化SignatureHelper实例用于设置参数，签名以及发送请求
        $helper = new AliyunSms();

        // 此处可能会抛出异常，注意catch
        $content = $helper->request(
            $accessKeyId,
            $accessKeySecret,
            "dysmsapi.aliyuncs.com",
            array_merge($params, array(
                "RegionId" => "cn-hangzhou",
                "Action" => "SendSms",
                "Version" => "2017-05-25",
            )),
            $security
        );
        $this->rest($content);
    }

    /**
     * 短信发送记录查询
     */
    function querySendDetails()
    {

        $params = array();

        // *** 需用户填写部分 ***
        // fixme 必填：是否启用https
        $security = false;

        // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
        $accessKeyId = "LTAIQipRRm7IRKRH";
        $accessKeySecret = "TI4043NRB1bMpKE6uVrJ4zldnF8Dti";

        // fixme 必填: 短信接收号码
        $params["PhoneNumber"] = "15085290559";

        // fixme 必填: 短信发送日期，格式Ymd，支持近30天记录查询
        $params["SendDate"] = "20181026";

        // fixme 必填: 分页大小
        $params["PageSize"] = 10;

        // fixme 必填: 当前页码
        $params["CurrentPage"] = 1;

        // fixme 可选: 设置发送短信流水号
//    $params["BizId"] = "yourBizId";

        // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***

        // 初始化SignatureHelper实例用于设置参数，签名以及发送请求
        $helper = new AliyunSms();

        // 此处可能会抛出异常，注意catch
        $content = $helper->request(
            $accessKeyId,
            $accessKeySecret,
            "dysmsapi.aliyuncs.com",
            array_merge($params, array(
                "RegionId" => "cn-hangzhou",
                "Action" => "QuerySendDetails",
                "Version" => "2017-05-25",
            )),
            $security
        );

        $this->rest($content);
    }

    public function getRegion()
    {
        $data = model("Region")->field("*,Id as city")->where("LevelType", 0)->select();
        $this->rest($data);
    }
}